import {createStore} from 'vuex'
import {IClub, IMobileStore, IPagination, IUser, SingletonState} from "../../interface"

export default createStore<IMobileStore>({
    state: {
        debug:false,
        user: {
            user: {
                nickname: 'undefinded',
                avatar: 'http://tantsz.com/public/1620694914.jpg',
                gender: 1,
                init: SingletonState.notlogin,
                actor: 0,
            },
            user_info: {
                id: 0,
                nickname: 'undefinded',
                gender: 0,
                birthday: "2000-04-22T00:00:00+08:00",
                qq: "12345678",
                created_at: "2021-05-11T09:01:57.569+08:00",
                updated_at: "2021-05-11T09:01:57.569+08:00",
                avatar: {
                    id: 17,
                    name: "",
                    mime: "",
                    path: "http://tantsz.com/public/1620694914.jpg",
                    is_foreign: true,
                    is_directory: false,
                    parent: null,
                    children: null,
                    owner_club: null,
                    // description: null,
                    created_at: "2021-05-11T09:01:57.567+08:00",
                    updated_at: "2021-05-11T09:01:57.567+08:00",
                    // DeletedAt: null
                }
            }

        },
        club_manage: {
            clubs: [],
            init: false,
        },
        page: 1,
        actors: {
            association_leader: 2,
            admin: 1,
            student: 0,
        },
        applys: {
            apply_clubs: [],
            page: -1,
            init: true,
            lastPagination: null,
        },
        singleton: {
            applyClubsManageState: SingletonState.sleep,
            applyUserInfo: SingletonState.sleep,
        },
    },
    mutations: {
        /**
         * 初始化设置user数据，如果user已被设置过，则不在设置
         * @param state 默认
         * @param user 用户信息
         */
        setUserInit(state, user) {
            state.user = user;
        },
        setPage(state, page) {
            state.page = page
        },
        /**
         * 覆盖俱乐部列表信息
         * @param state
         * @param clubs
         */
        setClubs(state, clubs: IClub[]) {
            state.club_manage.clubs = clubs;
            state.club_manage.init = true;
        },
        changeApplyForClubsListState(state, status) {
            state.singleton.applyClubsManageState = status;
        },
        changApplyForUserInfoState(state, status) {
            state.singleton.applyUserInfo = status;
        },
        logined(state) {
            state.user.user.init = SingletonState.logined;
        },
        userExit(state) {
            state.singleton.applyUserInfo = SingletonState.sleep;
            state.user.user.init = SingletonState.notlogin;
        }


    },
    actions: {
        /**
         * 在这里 获取用户数据，初始化
         * @param context
         * @param id
         */
        initUser(context, id) {
        },

        getApplyClubsDate(context, id: number) {
            ;
        },
        async initAllAssociation(context) {
            if (context.state.singleton.applyClubsManageState == SingletonState.doing
                || context.state.singleton.applyClubsManageState == SingletonState.done) {
                return;
            }
            context.commit('changeApplyForClubsListState', SingletonState.doing)
            let flag: boolean = true;
            let clubs: IClub[] = [];
            let index: number = 0;
            let size: number = 20;
            while (flag) {
                let obj: any = await fetch("api/club/?page=" + index + "&size=" + size)
                let result: IPagination<any> = await obj.json();
                for (const resultElement of result.items) {
                    clubs.push({
                        id: resultElement.id,
                        desc: resultElement.introduction,
                        name: resultElement.name,
                        avatar: resultElement.favicon.path,
                        content: ""
                    });
                }
                if (!result.page_info.has_next_page) {
                    flag = false;
                }
            }
            context.commit('setClubs', clubs);
            context.commit('changeApplyForClubsListState', SingletonState.done);
        },
        async getUserInfo(context) {
            // console.log("init user info")
            if (context.state.singleton.applyUserInfo == SingletonState.sleep) {
                context.commit('changApplyForUserInfoState', SingletonState.doing);
                fetch("api/authorize/self").then(res => res.json()).then(json => {
                   if(context.state.debug){
                       console.log(json)
                   }
                    let user: IUser = {
                        nickname: json.nickname,
                        avatar: json.avatar.path,
                        gender: json.gender,
                        init: SingletonState.logined,
                        actor: 0,
                    };
                    let obj:any={
                        user:user,
                        user_info:json
                    }
                    context.commit('setUserInit', obj);
                    context.commit('changApplyForUserInfoState', SingletonState.done);
                })
            }

        }
    },
    modules: {}
})
